﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */


using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

using Twipler.App.DataDoc;

namespace Twipler.App.Render.Tree
{
    public class DocValueItem : AbstractItem
    {
        private string _name;
        private string _select;
        private string _format;

        public override List<AbstractItem> Children { get { return null; } }

        public static DocValueItem Create(string Name, string Select, string Format)
        {
            return new DocValueItem() { _name = Name, _select = Select, _format = Format };
        }

        public override void Render(System.Text.StringBuilder html, App.Machine.Environment environment)
        {
            if (!environment.Variables_Contains(_name))
                return;

            IDataDoc val = environment.Variables_Get(_name) as IDataDoc;
            if (val == null)
                html.AppendFormat("Variable {0} not defined or not wrong type.", _name);
            else
            {
                DataDocValue value = val.Find(_select);
                if (value == null)
                    html.AppendFormat("Cannot find {0} in {1}", _select, _name);
                else
                    RenderFormat(html, value, _format);}
        }

        private void RenderFormat(StringBuilder html, DataDocValue val, string format)
        {
            switch (format.ToLower())
            {
                case "since":
                    html.Append(FormatTimeSince(val));
                    break;

                case "urlenc":
                    html.Append(HttpUtility.UrlEncode(val.Value.ToString()));
                    break;

                case "sansnormal":
                    html.Append(val.Value.ToString().Replace("_normal", ""));
                    break;

                case "durty":
                    Effect.Durty.Render(html, val.Value as string);
                    break;

                case "{0}":
                    html.AppendFormat(_format, val.Value);
                    break;

                default:
                    DateTime date;
                    if (val.ValueToDateTime(out date))
                        html.AppendFormat(_format, date);
                    else
                        html.AppendFormat(_format, val.Value);
                    break;
            }
        }

        private string FormatTimeSince(DataDocValue val)
        {
            DateTime date;

            if (val.Value is DateTime)
                date = (DateTime)val.Value;
            else
                if (val.ValueToDateTime(out date) == false)
                    return "Invalid date format";

            TimeSpan span = DateTime.UtcNow.Subtract(date.AddSeconds(App.Current.UtcSecondsOffset));
            Current.UtcSecondsOffsetLowest = span;

            if (span.TotalDays > 30)
                return (date).ToShortDateString();

            if ((int)span.TotalHours > 24)
                return string.Format("{0}d {1}h ago", (int)span.TotalDays, (int)span.TotalHours % 24);

            if ((int)span.TotalMinutes > 60)
                return string.Format("{0}h {1}m ago", (int)span.TotalHours, (int)span.TotalMinutes % 60);

            if ((int)span.TotalMinutes >= 1)
                return string.Format("{0}m {1}s ago", (int)span.TotalMinutes, (int)span.TotalSeconds % 60);

            if (span.TotalSeconds < 0)
                span = TimeSpan.Zero;
            
            return string.Format("{0} seconds ago", (int)span.TotalSeconds);
        }
    }
}
